/*
 * Copyright 2004-2005 公司名
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）* 复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.companyname.cn/licenses
 */

package com.companyname.common.templet;

import org.apache.log4j.Logger;
import org.easymock.EasyMock;
import org.hibernate.ObjectDeletedException;
import org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException;
import org.testng.Assert;

import com.companyname.common.base.dao.DAO;
import com.companyname.common.base.manager.Manager;
import com.companyname.common.base.model.BaseObject;


/**
 * manager单元测试用例模板
 *
 * @author amonlei
 *
 */

public abstract class ManagerTestCaseTemplet extends AbstractTestCaseTemplet
{
        static Logger logger = Logger.getLogger(ManagerTestCaseTemplet.class);
        
        public Manager getManagerInst(Class cls)
        {
                try
                {
                        return (Manager)cls.newInstance();
                }
                catch (Exception e)
                {
                        Assert.fail("实例化manager(" + cls.getName() + ")失败");
                        return null;
                } 
        }
        
        public void testSave(BaseObject savingBO,
                             BaseObject savedBO,
                             DAO dao,
                             Manager manager)
        {
                Assert.assertNotNull(savingBO);
                Assert.assertNotNull(savedBO);
                Assert.assertNotNull(dao);
                Assert.assertNotNull(manager);
                dao.save(savingBO);
                EasyMock.expectLastCall().andReturn(savedBO);
                // 回放,必须!!!!
                EasyMock.replay(dao);
                // 开始测试manager
                Assert.assertEquals(savedBO,manager.save(savingBO));
        }
        
        public void testUpdate(BaseObject updatingBO,
                               BaseObject updatedBO,
                               DAO dao,
                               Manager manager)
        {
                Assert.assertNotNull(updatingBO);
                Assert.assertNotNull(updatedBO);
                Assert.assertNotNull(dao);
                Assert.assertNotNull(manager);
                dao.update(updatingBO);
                EasyMock.expectLastCall().andReturn(updatedBO);
                // 回放,必须!!!!
                EasyMock.replay(dao);
                // 开始测试manager
                Assert.assertEquals(updatedBO,manager.update(updatedBO));
        }
        
        public void testRemove(BaseObject removingBO,
                               DAO dao,
                               Manager manager)
        {
                Assert.assertNotNull(removingBO);
                Assert.assertNotNull(dao);
                Assert.assertNotNull(manager);
                dao.remove(removingBO);
                EasyMock.expectLastCall();
                
                dao.get(removingBO.getId());
                EasyMock.expectLastCall()
                        .andThrow(new HibernateObjectRetrievalFailureException(new ObjectDeletedException("已被删除",
                                                                                                          removingBO.getId(),
                                                                                                          removingBO.getClass()
                                                                                                                    .toString())));
                // 回放,必须!!!!
                EasyMock.replay(dao);
                // 开始测试manager
                manager.remove(removingBO);
                manager.get(removingBO.getId());
        }
        
        public void testGet(BaseObject gottenBO,
                            DAO dao,
                            Manager manager)
        {
                Assert.assertNotNull(gottenBO);
                Assert.assertNotNull(dao);
                Assert.assertNotNull(manager);
                dao.get(gottenBO.getId());
                EasyMock.expectLastCall().andReturn(gottenBO);
                // 回放,必须!!!!
                EasyMock.replay(dao);
                // 开始测试manager
                Assert.assertNotNull(manager.get(gottenBO.getId()));
        }
        
        public void testGetByCode(BaseObject gottenBO,
                                  DAO dao,
                                  Manager manager)
        {
                Assert.assertNotNull(gottenBO);
                Assert.assertNotNull(dao);
                Assert.assertNotNull(manager);
                dao.getByCode(gottenBO.getCode());
                EasyMock.expectLastCall().andReturn(gottenBO);
                // 回放,必须!!!!
                EasyMock.replay(dao);
                // 开始测试manager
                Assert.assertNotNull(manager.getByCode(gottenBO.getCode()));
        }
}
